<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.name" placeholder="用户名" class="handle-input handle-input_min mr10"></el-input>
                <el-select v-model="query.sex" placeholder="性别" class="handle-select mr10">
                    <el-option key="1" label="请选择性别" value=""></el-option>
                    <el-option key="2" label=" 男" value="0"></el-option>
                    <el-option key="3" label="女" value="1"></el-option>
                </el-select>
                <el-select v-model="query.education" placeholder="学历" class="handle-select mr10">
                    <el-option key="1" label="请选择学历" value=""></el-option>
                    <el-option key="2" label="小学" value="小学"></el-option>
                    <el-option key="3" label="初中" value="初中"></el-option>
                    <el-option key="4" label="高中" value="高中"></el-option>
                    <el-option key="5" label="本科" value="本科"></el-option>
                    <el-option key="6" label="硕士" value="硕士"></el-option>
                    <el-option key="7" label="博士" value="博士"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="30" align="center" prop="uidInfo"></el-table-column>
                <el-table-column prop="userName" label="用户名" width="200"  align="center"></el-table-column>
                <el-table-column prop="a" label="号码" align="center"></el-table-column>
                <el-table-column label="性别" align="center"> <!--prop="registerStatus"-->
                    <template slot-scope="scope">
                        <p v-if="scope.row.sex=='0'">男</p>
                        <p v-if="scope.row.sex=='1'">女</p>
                    </template>
                </el-table-column>
                <el-table-column  label="生日" align="center">
                    <template slot-scope="scope">{{scope.row.createDate | formatDate}}</template>
                </el-table-column>
                <el-table-column prop="education" label="学历" align="center"></el-table-column>
                <el-table-column label="区域" align="center" prop="address"> <!--prop="registerStatus"-->
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-lx-calendar"
                                class="red"
                                @click="handleDetail(scope.$index, scope.row)"
                        >详情</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="详情" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" :rules="formRules" label-width="100px">
                <el-form-item label="姓名" required prop="userName">
                    <el-input v-model="form.userName"  ></el-input>
                </el-form-item>
                <el-form-item label="区域" required prop="address">
                    <area-cascader  v-model="form.address" :data="$pcaa" placeholder="区域" type="text" :level="1"></area-cascader>
                </el-form-item>
                <el-form-item label="性别" required prop="sex">
                    <el-select v-model="form.sex" placeholder="性别" class="handle-select mr10">
                        <el-option key="1" label="请选择性别" value=""></el-option>
                        <el-option key="2" label=" 男" value="0"></el-option>
                        <el-option key="3" label="女" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学历" required prop="education">
                    <el-select v-model="form.education"  placeholder="学历" class="handle-select mr10">
                        <el-option key="1" label="请选择学历" value=""></el-option>
                        <el-option key="2" label="小学" value="小学"></el-option>
                        <el-option key="3" label="初中" value="初中"></el-option>
                        <el-option key="4" label="高中" value="高中"></el-option>
                        <el-option key="5" label="本科" value="本科"></el-option>
                        <el-option key="6" label="硕士" value="硕士"></el-option>
                        <el-option key="7" label="博士" value="博士"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" required prop="birthday">
                    <el-date-picker
                            v-model="form.birthday"
                            align="right"
                            type="date"
                            placeholder="选择日期"
                            prop="birthday"
                            :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">保存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../common/date.js';
    import Vue from 'vue';
    import { pcaa } from 'area-data-vue';
    import 'area-linkage-vue/dist/index.css';
    import AreaLinkageVue from 'area-linkage-vue';
    Vue.prototype.$pcaa = pcaa;
    Vue.use(AreaLinkageVue)
    export default {
        name: "UserTable",
        data() {
            return {
                query: {
                    name: '',
                    region: '',
                    sex:'',
                    education:'',
                    pageIndex: 0,
                    pageSize: 5

                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 10,
                form: {},
                idx: -1,
                id: -1,
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },formRules: {
                    userName: [
                        { required: true, message: '请输入昵称名称', trigger: 'blur' },
                        { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    sex: [
                        { required: true, message: '请选择性别', trigger: 'change' }
                    ],
                    education: [
                        { required: true, message: '请选择', trigger: 'change' }
                    ],
                    address: [
                        { required: true, message: '请选择地址', trigger: 'change' }
                    ],
                    birthday:[
                        { required: true, message: '请选择日期', trigger: 'change' }
                    ]
                },

            };
        },
        created() {
            this.getData(this.query);
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                const _this = this;
                _this.$axios.post("/userinfo/users",_this.query).then(res => {
                    _this.tableData =res.data.data.userinfoList;
                    _this.pageTotal=res.data.data.totalCount;


            })
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 0);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                    this.$axios.get("/userinfo/del/"+row.uidInfo).then(res =>{
                    if(res.status = 200){
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                    this.getData();
                }

            });
            })
            .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                    this.$axios.get("/userinfo/del/"+this.multipleSelection[i].uidInfo).then(res =>{
                        if(res.status = 200){
                        this.tableData.splice(this.multipleSelection[i], 1);
                        this.getData();
                    }

                });
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                const _this=this;
                _this.idx = index;
                console.log(row.uidInfo)
                this.$axios.get('/userinfo/' + row.uidInfo).then((res) => {
                    _this.form=res.data.data;
            })
                this.editVisible = true;
            },

            //详情
            handleDetail(index, row) {
                const _this=this;
                _this.idx = index;
                this.$axios.get('/userinfo/' + row.uidInfo).then((res) => {
                _this.form=res.data.data;
            })
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit(formName) {
                const _this = this;
                console.log( _this.form);
                _this.$refs[formName].validate((valid) => {
                    if (valid) {
                       console.log( _this.form);
                        _this.form.address= _this.form.address.toString();
                        _this.$axios.post("/userinfo/saveEdit", _this.form).then(res => {
                            this.editVisible = false;
                        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                        this.$set(this.tableData, this.idx, this.form);
                        });
                    }
                        console.log('error submit!!');
                        return false;
            });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }

        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }


    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .handle-input_min{
        width: 150px;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
